import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import styleImport from 'vite-plugin-style-import'
import viteCompression from 'vite-plugin-compression'

const resolve = dir => require('path').join(__dirname, dir)

export default defineConfig({
  base: './', // 设置打包路径
  plugins: [
    vue(),
    // 样式按需引入
    styleImport({
      libs: [
        {
          libraryName: 'element-plus',
          esModule: true,
          ensureStyleFile: true,
          resolveStyle: name => {
            return `element-plus/packages/theme-chalk/src/${name.slice(3)}.scss`
          },
          resolveComponent: name => {
            return `element-plus/lib/${name}`
          }
        }
      ]
    }),
    // Gzip压缩
    viteCompression({
      verbose: true,
      disable: false,
      threshold: 10240,
      algorithm: 'gzip',
      ext: '.gz'
    })
  ],
  resolve: {
    // 别名配置
    alias: {
      '@': resolve('src'),
      '@i': resolve('src/api')
    }
  },
  // 启动服务配置
  server: {
    host: '0.0.0.0',
    cors: true,
    port: 9527
    // open: false,
    // https: false,
    // proxy: {
    //   // 选项写法
    //   '/api': {
    //     target: 'http://xxx:xxx',
    //     changeOrigin: true,
    //     rewrite: path => path.replace(/^\/api/, '')
    //   }
    // }
  },
  build: {
    // 去除生产环境控制台的输出
    terserOptions: {
      compress: {
        drop_console: true,
        drop_debugger: true
      }
    }
  }
})
